package android.common;

import java.util.List;

import org.apache.http.Header;

import com.fasterxml.jackson.jr.ob.JSON;
import com.loopj.android.http.TextHttpResponseHandler;

public abstract class JsonListRequestHandler<T> extends TextHttpResponseHandler {

	private Class<T> clazz;

	public JsonListRequestHandler() {

	}

	public JsonListRequestHandler(Class<T> clazz) {
		this.setClazz(clazz);
	}

	@Override
	public void onFailure(int status, Header[] headers, String result,
			Throwable error) {
		error.printStackTrace();
	}

	@Override
	public final void onSuccess(int status, Header[] headers, String result) {
		try {
			List<T> res = null;
			if (result != null && clazz != Void.class)
				res = JSON.std.listOfFrom(getClazz(), result);
			onSuccess2(status, headers, res);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public abstract void onSuccess2(int status, Header[] headers, List<T> result);

	public Class<T> getClazz() {
		return clazz;
	}

	public void setClazz(Class<T> clazz) {
		this.clazz = clazz;
	}
}
